<template>
  <t-dialog :header="`${header}【当前已勾选${selectedRowKeys.concat(selectedRowKeys2).length}】【待评审项目数${projectList.length}】`"
    @confirm="onSubmit" :confirmBtn="{
      content: loading ? '提交中...' : '确认',
      theme: 'primary',
      loading: loading,
    }" :visible.sync="visible" :onClose="closeDialog" :closeOnOverlayClick="false" width="80%" top="5%">
    <div style="height: 65vh">
      <div
        style="display: flex;font-weight: bold;justify-content: center;align-items: center;font-size: 30px;padding: 30px">
        推荐项目列表 <t-button @click="getSmartList" style="margin-left: 20px" variant="outline" theme="primary">
          <t-icon name="refresh" slot="icon" />
          刷新
        </t-button>
      </div>
      <t-form ref="form" :data="queryParams1" label-width="80"
        @submit="() => { this.pagination.current = 1; this.queryParams1.pageNum = 1; this.getSmartList() }"
        @reset="() => { this.pagination.current = 1; this.queryParams1.pageNum = 1; this.getSmartList() }">
        <t-row style="border-bottom: 1px solid #E5E6EB;margin-bottom: 10px">
          <t-col :span="11" class="query-form-inline">

            <t-form-item label="项目名称" name="projectName">
              <t-input v-model="queryParams1.projectName" clearable placeholder="请输入项目名称" style="width: 240px" />
            </t-form-item>
            <t-form-item label="学科分类" name="subjectClassification">
              <t-select style="width: 240px" clearable v-model="queryParams1.subjectClassification"
                :options="dict.type.subject_classification">
              </t-select>
            </t-form-item>
            <!-- <t-form-item label="计划类别" name="categoriesList" v-if="formData.reviewType != '5'">
              <t-select style="width: 240px" multiple clearable v-model="queryParams1.categoriesList"
                :options="dict.type.projectCategory">
              </t-select>
            </t-form-item> -->
            <t-form-item v-if="formData.reviewType == '5'" label="计划类别" name="categoriesList">
              <t-tree-select filterable expand-all multiple clearable v-model="queryParams1.categoriesList"
                style="width: 240px" :treeProps="{ keys: { value: 'id', label: 'name' } }"
                :data="projectCategoryOptions"></t-tree-select>
            </t-form-item>


          </t-col>
          <t-col :span="1" class="operation-container">
            <t-button theme="primary" type="submit"> 查询</t-button>
            <t-button theme="default" variant="outline" type="reset">重置</t-button>
          </t-col>
        </t-row>
      </t-form>
      <t-table row-key="id" :columns="formData.reviewType == 5 ? columns2 : columns" :data="data2"
        :selected-row-keys="selectedRowKeys2" :loading="dataLoading2"
        @select-change="(val) => { selectedRowKeys2 = val }">
        <template #[`subjectClassification`]="{ row }">
          {{ row.subjectClassification | filterByDict(dict.type.subject_classification) }}
        </template>
        <template #[`projectType`]="{ row }">
          {{ row.projectType | filterByDict(dict.type.project_type) }}
        </template>

        <template #op="{ row }">
          <t-button v-if="row.version === '2024'"
            @click="dialog.header = row.projectName; dialog.objId = row.id; dialog.editOpen = false; dialog.open_item_detail = true;"
            size="small" variant="text" class="t-button-link">
            <t-icon name="info-circle" slot="icon" />
            详情
          </t-button>
          <t-button v-else
            @click="dialog.header = row.projectName; dialog.objId = row.id; dialog.editOpen = false; dialog.open_detail = true"
            size="small" variant="text" class="t-button-link">
            <t-icon name="info-circle" slot="icon" />
            详情
          </t-button>
        </template>
      </t-table>
      <div style="display: flex;font-weight: bold;justify-content: center;font-size: 30px;padding: 30px">
        项目列表
      </div>

      <t-form ref="form" :data="queryParams" label-width="80"
        @submit="() => { this.pagination.current = 1; this.queryParams.pageNum = 1; this.getList() }"
        @reset="() => { this.pagination.current = 1; this.queryParams.pageNum = 1; this.getList() }">
        <t-row style="border-bottom: 1px solid #E5E6EB;margin-bottom: 10px">
          <t-col :span="11" class="query-form-inline">

            <t-form-item label="项目名称" name="projectName">
              <t-input v-model="queryParams.projectName" clearable placeholder="请输入项目名称" style="width: 240px" />
            </t-form-item>
            <t-form-item label="学科分类" name="subjectClassification">
              <t-select style="width: 240px" clearable v-model="queryParams.subjectClassification"
                :options="dict.type.subject_classification">
              </t-select>
            </t-form-item>
            <t-form-item v-if="formData.reviewType == '5'" label="计划类别" name="categoriesList">
              <t-tree-select filterable expand-all multiple clearable v-model="queryParams.categoriesList"
                style="width: 240px" :treeProps="{ keys: { value: 'id', label: 'name' } }"
                :data="projectCategoryOptions"></t-tree-select>
            </t-form-item>

          </t-col>
          <t-col :span="1" class="operation-container">
            <t-button theme="primary" type="submit"> 查询</t-button>
            <t-button theme="default" variant="outline" type="reset">重置</t-button>
          </t-col>
        </t-row>
      </t-form>
      <t-table row-key="id" :columns="columns" :data="data" :selected-row-keys="selectedRowKeys" :loading="dataLoading"
        @select-change="(val) => { selectedRowKeys = val }">
        <template #[`subjectClassification`]="{ row }">
          {{ row.subjectClassification | filterByDict(dict.type.subject_classification) }}
        </template>
        <template #[`projectType`]="{ row }">
          {{ row.projectType | filterByDict(dict.type.project_type) }}
        </template>

        <template #op="{ row }">
          <t-button v-if="row.version === '2024'"
            @click="dialog.header = row.projectName; dialog.objId = row.id; dialog.editOpen = false; dialog.open_item_detail = true"
            size="small" variant="text" class="t-button-link">
            <t-icon name="info-circle" slot="icon" />
            详情
          </t-button>
          <t-button v-else
            @click="dialog.header = row.projectName; dialog.objId = row.id; dialog.editOpen = false; dialog.open_detail = true"
            size="small" variant="text" class="t-button-link">
            <t-icon name="info-circle" slot="icon" />
            详情
          </t-button>
        </template>
      </t-table>
    </div>
    <project-detail-dialog :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_detail"
      v-if="dialog.open_detail"></project-detail-dialog>
    <item-detail-dialog :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_item_detail"
      v-if="dialog.open_item_detail"></item-detail-dialog>

  </t-dialog>
</template>

<script>
import ProjectDetailDialog from "../../pm/components/project-detail-dialog.vue";
import ItemDetailDialog from "../../pm/components/item-detail-dialog.vue";

export default {
  name: "select-project",
  dicts: ["subject_classification", "project_type", "subject_classification"],
  components: {
    ProjectDetailDialog,
    ItemDetailDialog
  },
  props: {
    header: {
      type: String,
      default: ''
    },
    formData: {
      type: Object,
      default: () => ({})
    },
    projectList: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      projectCategoryOptions: [],
      loading: false,
      data2: [],
      dataLoading2: false,
      selectedRowKeys2: [],
      dialog: {
        header: "",
        objId: 0,
        open_detail: false,
        editOpen: true,
        open_item_detail: false,
      },
      selectedRowKeys: [],
      queryParams: {
        reviewType: '',
        pageNum: 1,
        subjectClassification: '',
        categoriesList: [],
        projectName: '',
        pageSize: 10,
        dateRange: []
      },
      queryParams1: {
        reviewType: '',
        pageNum: 1,
        subjectClassification: '',
        categoriesList: [],
        projectName: '',
        pageSize: 10,
        dateRange: []
      },
      data: [],
      dataLoading: false,
      pagination: {
        defaultCurrent: 1,
        defaultPageSize: 10,
        total: 0,
        showPageSize: false,
        showFirstAndLastPageBtn: false,
      },
      columns: [
        {
          colKey: 'row-select',
          type: 'multiple',
          className: 'demo-multiple-select-cell',
          width: 64,
        },
        {
          colKey: "projectName",
          title: "项目名称",
          align: "center",
        },
        {
          colKey: "projectLeaderName",
          title: "负责人",
          align: "center",
          width: 100
        },
        {
          colKey: "declareDeptName",
          title: "承担单位",
          align: "center",
          width: 180,
        },
        {
          colKey: "adviceFunding",
          title: "建议经费总概算",
          align: "center",
          width: 140
        },
        {
          colKey: "subsidyFunding",
          title: import.meta.env.VITE_SITE_TAG === 'jtt' ? "厅拨经费" : `集团补助经费`,
          align: "center",
          width: 140
        },
        {
          colKey: "projectCategoryName",
          title: "计划类别",
          align: "center",
          width: 120
        },
        {
          colKey: "subjectClassification",
          title: "学科分类",
          align: "center",
          width: 100
        },
        // {
        //   colKey: "projectType",
        //   title: "项目类型",
        //   align: "center",
        //   width: 120
        // },

        // {
        //   colKey: "createTime",
        //   title: "申报时间",
        //   align: "center",
        //   width: 200
        // },
        {
          colKey: "op",
          title: "操作",
          align: "center",
          width: 120
        },
      ],

      columns2: [
        {
          colKey: 'row-select',
          type: 'multiple',
          className: 'demo-multiple-select-cell',
          width: 64,
        },
        {
          colKey: "projectName",
          title: "项目名称",
          align: "center",
        },
        {
          colKey: "projectLeaderName",
          title: "负责人",
          align: "center",
          width: 100
        },
        {
          colKey: "declareDeptName",
          title: "承担单位",
          align: "center",
          width: 180,
        },
        {
          colKey: "adviceFunding",
          title: "建议经费总概算",
          align: "center",
          width: 140
        },
        {
          colKey: "subsidyFunding",
          title: import.meta.env.VITE_SITE_TAG === 'jtt' ? "厅拨经费" : `集团补助经费`,
          align: "center",
          width: 140
        },
        {
          colKey: "projectCategoryName",
          title: "计划类别",
          align: "center",
          width: 120
        },
        {
          colKey: "subjectClassification",
          title: "学科分类",
          align: "center",
          width: 100
        },
        // {
        //   colKey: "projectType",
        //   title: "项目类型",
        //   align: "center",
        //   width: 120
        // },
        // {
        //   colKey: "createTime",
        //   title: "申报时间",
        //   align: "center",
        //   width: 200
        // },

        {
          colKey: "op",
          title: "操作",
          align: "center",
          width: 120
        },
      ],
      visible: true,
    }
  },
  watch: {
    formData: {
      immediate: true,
      handler (val) {
        if (val.id) {
          this.getList();
          this.getDeclareTree();
          this.getSmartList();
        }
      }
    }
  },
  methods: {
    getDeclareTree () {
      this.$api.kjxm.project
        .planCategoryNoChildList()
        .then((response) => {
          this.projectCategoryOptions = this.handleTree(response.data, 'id');
        })
        .catch((e) => {
          this.$message.error(e.toString());
        });
    },
    getList () {
      this.dataLoading = true;
      // this.queryParams.pageNum = this.pagination.current || 1;
      this.queryParams.reviewType = this.formData.reviewType;
      this.queryParams.teamId = this.formData.id;
      this.$api.kjxm.review.teamProjectListByReviewType(this.queryParams).then((res) => {
        this.data = res.data;
        this.dataLoading = false;
      }).catch((e) => {
        this.dataLoading = false
        this.$message.error(e.toString());
      });
    },
    getSmartList () {
      this.dataLoading2 = true;
      // this.queryParams.pageNum = this.pagination.current || 1;
      this.queryParams1.reviewType = this.formData.reviewType;
      this.queryParams1.teamId = this.formData.id;
      this.$api.kjxm.review.teamSmartProjectListByReviewType(this.queryParams1).then((res) => {
        this.data2 = res.data;
        this.dataLoading2 = false;
      }).catch((e) => {
        this.dataLoading2 = false
        this.$message.error(e.toString());
      });
    },
    onSubmit () {

      if (this.selectedRowKeys.concat(this.selectedRowKeys2).length === 0) {
        this.$message.error('请选择项目')
        return false
      }
      const businessId = this.selectedRowKeys.concat(this.selectedRowKeys2).map(val => ({
        teamId: this.formData.id,
        businessId: val,
        reviewType: this.formData.reviewType
      }))
      this.loading = true;
      this.$api.kjxm.review.teamProjectAddList(businessId).then((res) => {
        // 初始化
        this.$message.success("选择成功");
        this.$emit("reload");
        this.$emit("update:visible", false);
        this.loading = false;
      }).catch((e) => {
        this.loading = false;
        this.$message.error(e.toString());
      });
    },
    closeDialog () {
      this.$emit("update:visible", false);
    },

  }
}
</script>

<style scoped>
.overlay-options {
  display: inline-block;
  font-size: 20px;
}
</style>
